package com.dp.service.jdc;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.CommentContent;
import com.dp.pojo.PageCtrl;
import com.dp.pojo.Weather;
import com.dp.service.impl3.BaseImpl;
import com.dp.vo.TChannels;

@Service
@Transactional
public class MessageServiceImpl extends BaseImpl {

    @SuppressWarnings("unchecked")
    public List<CommentContent> getMessage(int pageNo,int pageSize){
        int resultLimtBegin =  pageSize*pageNo;
        String sql = "SELECT nc.id,nc.cid,nc.channleid,nc.clientid,nc.commenttext,nc.name,nc.commenttime,nc.isgood,nc.isdelete,nc.deleter,nc.deletetime,nc.auditer,nc.audittime" +
                " FROM news_comment nc where  nc.channleid=1 order by nc.cid desc limit "+resultLimtBegin+","+pageSize;
        List<CommentContent> result = new ArrayList();
        CommentContent comment = null;
        try {
            List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
            for (Object[]o: temp) {
                comment = new CommentContent();
                
                comment.setId(Integer.parseInt(this.getString(o[0])));
                comment.setCid(Integer.parseInt(this.getString(o[1])));
                comment.setChannleid(Integer.parseInt(this.getString(o[2])));
                comment.setClientid(this.getString(o[3]));
                comment.setCommenttext(this.getString(o[4]));
                comment.setName((this.getString(o[5])));
                comment.setCommenttime(this.getString(o[6]).substring(0, 19));
                result.add(comment);
                
            }
            
            
        } catch (Exception e) {
            log.error("MessageServiceImpl-->>getMessage fail !!!" +e.getMessage());
        }
    
        return result;
        
    }

    
    public boolean addMessage(CommentContent comment){
        this.sql="insert into news_comment (name,commenttime,channleid,cid,commenttext,clientid,deleter,auditer) values (?,?,?,?,?,?,?,?)";
        try {
            Query   query = em.createNativeQuery(sql);
            
            query.setParameter(1, comment.getName());
            
            DateFormat format2 = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
            Date date = format2.parse(comment.getCommenttime());
            query.setParameter(2, date);
            query.setParameter(3, comment.getChannleid());
            query.setParameter(4, comment.getCid());
            query.setParameter(5, comment.getCommenttext());
            query.setParameter(6, comment.getClientid());
            query.setParameter(7, comment.getDeleter());
            query.setParameter(8, comment.getAuditer());
            
            
            query.executeUpdate();
            return true;
        } catch (ParseException e) {
            log.error("MessageServiceImpl-->>getMessage fail !!!" +e.getMessage());
            return false;
        }
    }
    
    @SuppressWarnings("unchecked")
    public CommentContent getMessageReply(int commentCid){
        String sql = "SELECT nc.id,nc.cid,nc.channleid,nc.clientid,nc.commenttext,nc.name,nc.commenttime,nc.isgood,nc.isdelete,nc.deleter,nc.deletetime,nc.auditer,nc.audittime" +
                " FROM news_comment nc where  nc.channleid=2 and nc.cid="+commentCid;    
        CommentContent comment = null;
        try {
            List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
            if(temp.size()!=0&&temp!=null){
            Object[] o = temp.get(0);
                comment = new CommentContent();
                
                comment.setId(Integer.parseInt(this.getString(o[0])));
                comment.setCid(Integer.parseInt(this.getString(o[1])));
                comment.setChannleid(Integer.parseInt(this.getString(o[2])));
                comment.setClientid(this.getString(o[3]));
                comment.setCommenttext(this.getString(o[4]));
                comment.setName((this.getString(o[5])));
                comment.setCommenttime(this.getString(o[6]));
                return comment;
            }
        } catch (Exception e) {
            log.error("MessageServiceImpl-->>getMessage fail !!!" +e.getMessage());
        }
    
        return comment;
        
    }
    public PageCtrl getPageCtrl(int pageNo,int pageSize){
        PageCtrl pageCtrl = new PageCtrl();
        String sql = "SELECT count(*) " +
                " FROM news_comment nc where  nc.channleid=1";
        try{
            int resultCount = Integer.valueOf( this.em.createNativeQuery(sql).getSingleResult().toString());
            pageCtrl.setResultCount(resultCount);
            pageCtrl.setPageNo(pageNo);
            pageCtrl.setPageSize(pageSize);
            pageCtrl.setPageCount(resultCount%pageSize==0?resultCount/pageSize:resultCount/pageSize+1);
            return pageCtrl;
            
        }catch (Exception e) {
            logger.error(" MessageServiceImpl-->>getPageCtrl fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
        
        
    }
}